// Package api
// @Title api[简述该包的主要作用]
// @Description
// @Author  cnHuaShao
// @since v0.0.1
// @Version v1.0.0
// @Date 2024/6/17 23:46
// @Update 2024/6/17 23:46
package api

import (
	"FinancialManagement/src/base/mess"
	"FinancialManagement/src/base/util"
	"FinancialManagement/src/business/product"
	"database/sql"
	"github.com/gin-gonic/gin"
	"gopkg.in/guregu/null.v3"
	"net/http"
)

// CreateProductInfo
// @Description: 创建产品信息
// @param c
func CreateProductInfo(c *gin.Context) {
	productName := c.DefaultPostForm("productName", "")
	productDesc := c.DefaultPostForm("productDesc", "")
	productInfo := product.ProductModel{
		ProductName: productName,
		ProductDesc: null.String{
			sql.NullString{
				String: productDesc,
				Valid:  true,
			},
		},
	}
	if err := productInfo.CreateProductInfo(); err != nil {
		c.JSON(200, mess.ResErrMess(910005101, err.Error()))
		return
	}
	c.JSON(200, mess.ResMessOK(""))
}

// UpdateProductInfo
// @Description: 修改指定产品信息
// @param c
func UpdateProductInfo(c *gin.Context) {
	productId := util.ToInt(c.DefaultPostForm("productId", "0"))
	productName := c.DefaultPostForm("productName", "")
	productDesc := c.DefaultPostForm("productDesc", "")
	if productId == 0 || productName == "" {
		c.JSON(http.StatusOK, mess.ResErrMess(910005102, "参数错误"))
		return
	}
	productInfo := product.ProductModel{
		ProductId:   productId,
		ProductName: productName,
		ProductDesc: null.String{
			sql.NullString{
				String: productDesc,
				Valid:  true,
			},
		},
	}
	if err := productInfo.UpdateProductInfo(); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005103, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// GetProductList
// @Description: 获取所有产品信息
// @param c
func GetProductList(c *gin.Context) {
	productName := c.DefaultPostForm("productName", "")
	pageSize := util.ToInt(c.DefaultPostForm("pageSize", "10"))
	pageNumber := util.ToInt(c.DefaultPostForm("pageNumber", "1"))
	productList, count, err := product.GetProductList(productName, pageSize, pageNumber)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005104, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(gin.H{
		"list":  productList,
		"count": count,
	}))
}

// GetProductListAll
// @Description:
// @param c
func GetProductListAll(c *gin.Context) {
	productName := c.DefaultPostForm("productName", "")
	productList, err := product.GetProductListAll(productName)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005104, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(productList))
}

// GetProductListForPurchaseId
// @Description: 获取指定采购单号下的产品信息
// @param c
func GetProductListForPurchaseId(c *gin.Context) {
	purchaseId := util.ToInt(c.DefaultPostForm("purchaseId", "0"))
	if purchaseId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005104, "采购单号缺失"))
		return
	}
	productList, err := product.GetProductListForPurchaseId(purchaseId)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005104, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(productList))
}

// DelProductInfo
// @Description: 删除指定产品信息
// @param c
func DelProductInfo(c *gin.Context) {
	productId := util.ToInt(c.DefaultPostForm("productId", "0"))
	if productId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005105, "参数错误"))
		return
	}
	if err := product.DeleteProductInfo(productId); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005106, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// GetProductInfo
// @Description: 获取指定产品信息
// @param c
func GetProductInfo(c *gin.Context) {
	productId := util.ToInt(c.DefaultPostForm("productId", "0"))
	if productId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005107, "参数错误"))
		return
	}
	productInfo, err := product.GetProductInfo(productId)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005108, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(productInfo))
}
